home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / BufferedWindows / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  4.0 KB  |  108 lines

  1. /*
  2.     File:        main.c
  3.  
  4.     Contains:    A sample that demonstrates the use of QDFlushPortBuffer when dealing with
  5.                         Buffered windows under Mac OS X.
  6.  
  7.     Written by:     Karl Groethe
  8.  
  9.     Copyright:    Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.             You may incorporate this Apple sample source code into your program(s) without
  12.             restriction. This Apple sample source code has been provided "AS IS" and the
  13.             responsibility for its operation is yours. You are not permitted to redistribute
  14.             this Apple sample source code as "Apple sample source code" after having made
  15.             changes. If you're going to re-distribute the source, we require that you make
  16.             it clear in the source that the code was descended from Apple sample source
  17.             code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                         9/00    Created
  21. */
  22.  
  23. #include <Carbon/Carbon.h>
  24. #include <pthread.h>
  25.  
  26. #define kWindowWidth 300
  27. #define kWindowHeight 300
  28. #define kMaxVelocity 5
  29. #define kBallSize 16
  30. void* AnimationThread(void* input);
  31.  
  32. int main(int argc, char* argv[])
  33. {
  34.     IBNibRef         nibRef;
  35.     pthread_t        Animation;
  36.     CreateNibReference(CFSTR("main"), &nibRef);
  37.     SetMenuBarFromNib(nibRef, CFSTR("MainMenu"));
  38.     DisposeNibReference(nibRef);
  39.     //start an animation thread for our windows
  40.     pthread_create(&Animation,NULL,AnimationThread,NULL);
  41.     
  42.     RunApplicationEventLoop();
  43.     
  44.     pthread_cancel(Animation);//tell thread to stop animating
  45.     pthread_join(Animation,NULL);//wait for the thread to stop
  46.     
  47.     return 0;
  48. }
  49. void* AnimationThread(void* input)
  50. {
  51.     /*------------------------------------------------------
  52.         A thread for doing all our animation
  53.     --------------------------------------------------------*/
  54.     #pragma unused(input)
  55.     
  56.     const Rect defaultBounds={100,100,100+kWindowHeight,100+kWindowWidth};
  57.     const RGBColor kBallColor={0x0,0x0,0xD400};//blue
  58.     WindowRef BufferedWindow, BufferedWindowWithFlush;
  59.     
  60.     RgnHandle visibleRgn=NewRgn();
  61.     
  62.     Rect BallRect={kBallSize,kBallSize,2*kBallSize,2*kBallSize};
  63.     Point BallVelocity={Random()%kMaxVelocity+1,Random()%kMaxVelocity+1};    
  64.     CreateNewWindow (kDocumentWindowClass, 
  65.                      kWindowStandardHandlerAttribute,
  66.                      &defaultBounds,
  67.                      &BufferedWindow);
  68.     SetWindowTitleWithCFString(BufferedWindow,CFSTR("no flush"));
  69.     CreateNewWindow (kDocumentWindowClass,
  70.                      kWindowStandardHandlerAttribute,
  71.                      &defaultBounds,
  72.                      &BufferedWindowWithFlush);
  73.     SetWindowTitleWithCFString(BufferedWindowWithFlush,CFSTR("Flush"));
  74.     ShowWindow(BufferedWindow);
  75.     ShowWindow(BufferedWindowWithFlush);
  76.     while(TRUE)//loop until thread is canceled
  77.     {
  78.         Rect animationBounds;
  79.         GrafPtr    origPort;
  80.         GetPort(&origPort);
  81.         SetPortWindowPort(BufferedWindow);
  82.         
  83.         GetPortBounds(GetWindowPort(BufferedWindow),&animationBounds);
  84.         if(BallRect.top<animationBounds.top||BallRect.bottom>animationBounds.bottom)
  85.             BallVelocity.v=-BallVelocity.v;
  86.         if(BallRect.left<animationBounds.left || BallRect.right>animationBounds.right)
  87.             BallVelocity.h=-BallVelocity.h;
  88.             
  89.         OffsetRect(&BallRect,BallVelocity.h,BallVelocity.v);
  90.         //draw in the window we don't flush
  91.         EraseRect(&animationBounds);
  92.         RGBForeColor(&kBallColor);
  93.         PaintOval(&BallRect);
  94.         //switch to the window we're going to flush
  95.         SetPortWindowPort(BufferedWindowWithFlush); 
  96.         EraseRect(&animationBounds);
  97.         RGBForeColor(&kBallColor);
  98.         PaintOval(&BallRect);
  99.         
  100.         //we flush the window buffer by passing the window and its visible region
  101.         QDFlushPortBuffer(GetWindowPort(BufferedWindowWithFlush),
  102.                 GetPortVisibleRegion(GetWindowPort(BufferedWindowWithFlush),visibleRgn));
  103.         SetPort(origPort);
  104.         pthread_testcancel();//see if we want to quit
  105.         usleep(10000);//sleep for a bit
  106.     }
  107. }
  108.